﻿using Abp;
using Abp.Application.Services;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using FwProject.Com.Dto;
using FwProject.Commodity.Dto;
using FwProject.Manager;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.Commodity;
using FwProject.ShoppingMall.Shop;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
namespace FwProject.Com
{
    /// <summary>
    /// 评论信息
    /// </summary>
    public class CommodityReviewsAppService : FwProjectAppServiceBase, ICommodityReviewsAppService
    {
        private CommodityReviewsManager _commodityReviewsManager;
        private readonly IRepository<CommodityReviews, Guid> _repositoryCommodityReviews;
        private readonly IRepository<CommodityReviewsImage, Guid> _repositoryCommodityReviewsImage;
        private readonly IRepository<ShopProduct, Guid> _repositoryShopProduct;
        private readonly IRepository<ShopOrder, Guid> _repositoryShopOrder;
        /// <summary>
        /// 
        /// </summary>
        public IGuidGenerator _guid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IObjectMapper _map { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="commodityReviewsManager"></param>
        /// <param name="repositoryCommodityReviews"></param>
        /// <param name="repositoryShopProduct"></param>
        /// <param name="repositoryShopOrder"></param>
        public CommodityReviewsAppService(CommodityReviewsManager commodityReviewsManager,
            IRepository<CommodityReviews, Guid> repositoryCommodityReviews,
            IRepository<ShopProduct, Guid> repositoryShopProduct,
            IRepository<ShopOrder, Guid> repositoryShopOrder,
             IRepository<CommodityReviewsImage, Guid> repositoryCommodityReviewsImag
           )
        {
            _repositoryCommodityReviewsImage = repositoryCommodityReviewsImag;
            _repositoryShopOrder = repositoryShopOrder;
            _repositoryShopProduct = repositoryShopProduct;
            _repositoryCommodityReviews = repositoryCommodityReviews;
            _commodityReviewsManager = commodityReviewsManager;
            _guid = SequentialGuidGenerator.Instance;
        }
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns>评论id</returns>
        [AbpAuthorize]
        public async Task<Guid> CreateCommodityReviews(CommodityReviewsInput model)
        {
            var user= (await GetCurrentUserAsync());
            var m = _map.Map<CommodityReviews>(model);
            m.Id = _guid.Create();
            m.Reviewsid = AbpSession.UserId.Value;
            m.ReviceName = user.Name;
            m.UserFaceImage = user.Face;
            foreach (var item in m.CommodityReviewsListImage)
            {
                item.CommodityReviewsId = m.Id;
                item.Id = _guid.Create();
            }

            return await _commodityReviewsManager.CreateCommodityReviews(m);
        }
        /// <summary>
        /// 修改一个评论
        /// </summary>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task UpdateCommodityReviews(CommodityReviewsAppUpdateDto model)
        {
            var m = _repositoryCommodityReviews.Get(model.Id);
            m = _map.Map(model, m);
            await _commodityReviewsManager.UpdateCommodityReviews(m);
        }
        /// <summary>
        /// 获取用户评论的商品信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<IPagedList<CommodityReviewsProductShopDto>> GetCommodityReviewsProductShopDto(int page, int pageSize)
        {
            var list = _repositoryCommodityReviews.GetAll().Where(o => o.Reviewsid == AbpSession.UserId.Value && o.IsShow==true);
            var shopList = _repositoryShopProduct.GetAll().Where(o => o.IsGrounding == true);

            return await list.OrderByDescending(o => o.CreationTime).Join(shopList, t => t.Productid, k => k.Id, (o, m) => new CommodityReviewsProductShopDto
            {
                Id = o.Id,
                UserId = o.Reviewsid,
                ImagePic = m.ImagePic,
                ReviceName = o.ReviceName,
                RevicwsContent = o.RevicwsContent,
                CommentScore = o.CommentScore,
                ShopproductId = o.Productid,
                ShopTypeid = m.ShopTypeid,
                ProductName = m.ProductName,
                SpcemarketPrice = m.SpcecostPrice,
                IsGrounding = m.IsGrounding,
                Unit = m.Unit,
                SpceWeight = m.SpceWeight

            }).GroupJoin(_repositoryCommodityReviewsImage.GetAll(), o => o.Id, n => n.CommodityReviewsId, (o, n) => new CommodityReviewsProductShopDto
            {
                Id = o.Id,
                UserId = o.UserId,
                ImagePic = o.ImagePic,
                ReviceName = o.ReviceName,
                RevicwsContent = o.RevicwsContent,
                CommentScore = o.CommentScore,
                ShopproductId = o.ShopproductId,
                ShopTypeid = o.ShopTypeid,
                ProductName = o.ProductName,
                SpcemarketPrice = o.SpcemarketPrice,
                IsGrounding = o.IsGrounding,
                Unit = o.Unit,
                SpceWeight = o.SpceWeight,
                ImageDtos = _map.Map<List<CommodityReviewsImageDto>>(n)
            }).ToPagedListAsync(page, pageSize);
        }

        /// <summary>
        /// 获取商品评论信息基本信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="id"></param>
        /// <param name="CommentScore"></param>
        /// <returns></returns>
       
        public async Task<IPagedList<CommodityReviewsShopDetailsDto>> GetProductShopDetailsCommodityReviewsDto(int page, int pageSize, Guid id, int? CommentScore)
        {
            var list = _repositoryCommodityReviews.GetAll().Where(o => o.Productid == id && o.IsShow == true);
            if (CommentScore != null) {
                if(CommentScore==1)
                    list= list.Where(o => o.CommentScore == CommentScore);
                if (CommentScore == 2)
                    list= list.Where(o => o.CommentScore == 2);
                if (CommentScore == 2)
                    list= list.Where(o => o.CommentScore >= 3);
            }
               
             var imgs = _repositoryCommodityReviewsImage.GetAll();

            return await list.GroupJoin(_repositoryCommodityReviewsImage.GetAll(), o => o.Id, n => n.CommodityReviewsId, (o, n) => new CommodityReviewsShopDetailsDto
            {
                Id=o.Id,
               Reviewsid = o.Reviewsid,
               ReviceName = o.ReviceName,
               UserFaceImage = o.UserFaceImage,
               RevicwsContent=o.RevicwsContent,
               CommentScore=o.CommentScore,
               ServiceCommentScore=o.ServiceCommentScore,
               CreationTime=o.CreationTime,
               ImageDtos =_map.Map<List<CommodityReviewsImageDto>>(n)
            }).OrderByDescending(o=>o.CreationTime).ToPagedListAsync(page, pageSize);

        }

        /// <summary>
        /// 获取商品的评论信息信息
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="pageSize">条数</param>
        /// <param name="id">商品编号</param>
        /// <param name="CommentScore">评分</param>
        /// <returns></returns>
       
        public async Task<IPagedList<CommodityReviewsProductShopDto>> GetProductShopCommodityReviewsDto(int page, int pageSize, Guid id, int? CommentScore)
        {
            var list = _repositoryCommodityReviews.GetAll().Where(o => o.Productid == id && o.IsShow==true);
            if (CommentScore != null)
                list.Where(o => o.CommentScore == CommentScore);


            var shopList = _repositoryShopProduct.GetAll().Where(o => o.IsGrounding == true);

            return await list.OrderByDescending(o => o.CreationTime).Join(shopList, t => t.Productid, k => k.Id, (o, m) => new CommodityReviewsProductShopDto
            {
                Id = o.Id,
                UserId = o.Reviewsid,
                UserFaceImage = o.UserFaceImage,
                ImagePic =m.ImagePic,
                ReviceName = o.ReviceName,
                RevicwsContent = o.RevicwsContent,
                CommentScore = o.CommentScore,
                ServiceCommentScore = o.ServiceCommentScore,
                ShopproductId = o.Productid,
                ShopTypeid = m.ShopTypeid,
                ProductName = m.ProductName,
                SpcemarketPrice = m.SpcecostPrice,
                IsGrounding = m.IsGrounding,
                Unit = m.Unit,
                SpceWeight = m.SpceWeight
            }).GroupJoin(_repositoryCommodityReviewsImage.GetAll(), o => o.Id, n => n.CommodityReviewsId, (o, n) => new CommodityReviewsProductShopDto
            {
                Id = o.Id,
                UserId = o.UserId,
                ImagePic =o.ImagePic,
                UserFaceImage = o.UserFaceImage,
                ReviceName = o.ReviceName,
                RevicwsContent = o.RevicwsContent,
                CommentScore = o.CommentScore,
                ServiceCommentScore = o.ServiceCommentScore,
                ShopproductId = o.ShopproductId,
                ShopTypeid =o.ShopTypeid,
                ProductName = o.ProductName,
                SpcemarketPrice = o.SpcemarketPrice,
                IsGrounding =o.IsGrounding,
                Unit = o.Unit,
                SpceWeight = o.SpceWeight,
                ImageDtos = _map.Map<List<CommodityReviewsImageDto>>(n)
            }).ToPagedListAsync(page, pageSize);

        }

        /// <summary>
        /// 查看一个评论信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<CommodityReviewsDto> GetById(Guid id)
        {
            var model = await _repositoryCommodityReviews.GetAsync(id);
            return _map.Map<CommodityReviewsDto>(model);
        }

        /// <summary>
        /// 删除一个评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteCommodityReviews(Guid id)
        {
            var model = _repositoryCommodityReviews.Get(id);
            await _commodityReviewsManager.DeleteCommodityReviews(model);
        }

        #region 后台管理
        /// <summary>
        /// 获取评论信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<LayerDataBaseDto> GetCommodityReviewsLayerDataAsync(int page, int limit)
        {
            var pagelist = await _repositoryCommodityReviews.GetAll().Join(_repositoryShopProduct.GetAll(), t => t.Productid, k => k.Id, (o, m) => new CommodityReviewsOutputDto
            {
                Id = o.Id,
                ReviceName = o.ReviceName,
                CommentScore = o.CommentScore,
                CreationTime = o.CreationTime,
                IsShow = o.IsShow,
                ProductName = m.ProductName,
            }).OrderByDescending(o=>o.CreationTime).ToPagedListAsync(page, limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;
            return dto;
        }

        #endregion

    }
}
